package world.ztomorrow.common.config;

import feign.Logger;
import feign.RequestInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.DispatcherServlet;
import world.ztomorrow.common.constant.SystemConstant;
import world.ztomorrow.common.utils.UserHolder;

@ConditionalOnClass(DispatcherServlet.class)
@Configuration
public class DefaultFeignConfig {

    @Bean
    public Logger.Level feignLogLevel(){
        return Logger.Level.FULL;
    }

    @Bean
    public RequestInterceptor userInfoRequestInterceptor(){
        return template -> {
            Long authorId = UserHolder.getAuthorId();
            if(authorId != null) {
                template.header(SystemConstant.NOVEL_AUTHOR_KEY, authorId.toString());
            }

            Long userId = UserHolder.getUserId();
            if(userId != null) {
                template.header(SystemConstant.NOVEL_FRONT_KEY, userId.toString());
            }
        };
    }
}
